package com.photopicker.main.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.photopicker.R;
import com.photopicker.base.DefaultBaseAdapter;
import com.photopicker.customview.PhotoView;
import com.photopicker.util.ScreenUtil;

import java.io.File;
import java.util.List;


public class EvaluatePicGrdAdapter extends DefaultBaseAdapter<String> {
    DisplayImageOptions options;
    private OnItemClickListener onItemClickListener;

    public EvaluatePicGrdAdapter(Context context, List<String> list) {
        super(context, list);
        options = new DisplayImageOptions.Builder().
                showImageOnLoading(R.drawable.common_bg) // resource
                .showImageForEmptyUri(R.drawable.common_bg) // resource or
                .showImageOnFail(R.drawable.common_bg)
                .cacheInMemory(true).cacheOnDisk(true).build();
        ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(context));
    }

    /**
     * ListView Item设置
     */
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = getLayoutInflater().inflate(R.layout.item_published_grida, parent, false);
            holder = new ViewHolder();
            holder.image = (PhotoView) convertView.findViewById(R.id.item_grida_image);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.image.disenable();
        if (onItemClickListener != null) {
            holder.image.setTag(position);
            holder.image.setOnClickListener(onItemClickListener);
        }
       /* BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_4444;
        final Bitmap img = BitmapFactory.decodeFile(getItem(position), options);
        //ImageLoader.getInstance().displayImage(getItem(position), holder.image, options);
        ImageLoader.getInstance().loadImage(getItem(position),new SimpleImageLoadingListener(){
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                super.onLoadingComplete(imageUri, view, loadedImage);
                holder.image.setImageBitmap(img);
            }
        });*/
        File mFile=new File(getItem(position));
        //若该文件存在
        if (mFile.exists()) {
            Bitmap bitmap=BitmapFactory.decodeFile(getItem(position));
            holder.image.setImageBitmap(ratio(mFile.toString(),ScreenUtil.dip2px(context,100),ScreenUtil.dip2px(context,100)));
        }
        return convertView;
    }

    public class ViewHolder {
        public PhotoView image;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public static abstract class OnItemClickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            onItemClick(view, (int) view.getTag());
        }

        public abstract void onItemClick(View view, int position);
    }

    public Bitmap ratio(String imgPath, float pixelW, float pixelH) {
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        // 开始读入图片，此时把options.inJustDecodeBounds 设回true，即只读边不读内容
        newOpts.inJustDecodeBounds = true;
        newOpts.inPreferredConfig = Bitmap.Config.RGB_565;
        // Get bitmap info, but notice that bitmap is null now
        Bitmap bitmap = BitmapFactory.decodeFile(imgPath,newOpts);

        newOpts.inJustDecodeBounds = false;
        int w = newOpts.outWidth;
        int h = newOpts.outHeight;
        // 想要缩放的目标尺寸
        float hh = pixelH;// 设置高度为240f时，可以明显看到图片缩小了
        float ww = pixelW;// 设置宽度为120f，可以明显看到图片缩小了
        // 缩放比。由于是固定比例缩放，只用高或者宽其中一个数据进行计算即可
        int be = 1;//be=1表示不缩放
        if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放
            be = (int) (newOpts.outWidth / ww);
        } else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放
            be = (int) (newOpts.outHeight / hh);
        }
        if (be <= 0) be = 1;
        newOpts.inSampleSize = be;//设置缩放比例
        // 开始压缩图片，注意此时已经把options.inJustDecodeBounds 设回false了
        bitmap = BitmapFactory.decodeFile(imgPath, newOpts);
        // 压缩好比例大小后再进行质量压缩
//        return compress(bitmap, maxSize); // 这里再进行质量压缩的意义不大，反而耗资源，删除
        return bitmap;
    }
}